package com.itheima.springbootcondition2.condition;

import com.itheima.springbootcondition2.config.ConditionOnClass;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class ClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        try {
            //工程当中 不指定包名 jedis,mysql
            Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(ConditionOnClass.class.getName());
            String[] value = (String[]) annotationAttributes.get("value");
            for (String className : value) {
                Class.forName(className);
            }
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
}
